GBML manual / Lists

Lists

list is a row of items that grows and shrinks while the game runs - add to the end, remove from anywhere, up to a size you choose when you declare it.

What makes a list different

A list is a row of items where the number of items changes as the game runs. A pickup goes into the inventory, a used potion comes out, a new score joins a table. You add to the end, you remove from anywhere, and at any moment you can ask how many items are in it.

That is the difference from the array, the container it is closest to. An array has a fixed number of slots that never change. A list starts empty (or with a few items you give it) and gains and loses items over time. Both are read the same way, with [i], where i is the item's index: its position in the row, counting from 0. inventory[0] is the first item, inventory[1] the second.

Everything a list can do is on this page: fifteen functions and [i]. Pick a list when the order of the items matters and the count is not known ahead of time - an inventory, a high-score table, "the last few things that happened."

There is no list_get or list_set, because [i] already does both: inventory[2] reads the third item and inventory[2] = "key" changes it. The functions are for the things [i] cannot do

Declaring one

/// @type {list<string>}
/// @capacity 6
var inventory = ["sword"];

The /// @type line says what the list holds. list<string> is a list where every item is a piece of text; list<int> would hold whole numbers, list<ref<obj_enemy>> would hold references to enemies. Every item in one list has the same type. To keep several differently-typed values together in one item, see Structs in data containers.

The /// @capacity line says the most items the list can ever hold. Leave it off and you get 8. It can be any number: unlike the queue, map and set, a list has no rule about which sizes are allowed. The limit exists because the GBA has no spare memory to grow into. The list's whole space is set aside when the game is built, so the size has to be a real number you picked. An object that keeps a list in one of its variables carries its own copy of that space, so a large capacity on an object with many instances adds up.

The items in brackets after = are the starting contents. Start empty with []. A starting list with more items than the capacity is refused when the project is checked, before the game runs.

The same list can be written with no comment at all. Both of these are the list above, just empty:

var inventory: list<string, 6> = [];          // the type, with the size inside it
var inventory = list_create(6, "");           // a function: capacity, then an example

list_create's second value is only an example of what the list holds. It is never put in the list, which starts empty. A list that is never given a size - by a /// @capacity comment, a number in its type, or list_create - holds 8 items. The comment form and the written-out forms are all equal; see Writing a type out.

To make a list that starts full instead of empty, use list_create_fill: var names = list_create_fill(12, "HI"); is a list with room for 12 that already holds twelve "HI". Its value is both the example (it sets the kind of list) and the thing that is put in every place. It is list_create and list_fill in one line. Only the list has one: array_create and grid_create already start full.

Length and capacity are two different numbers

list_length is how many items are in the list right now. The capacity is the most it could hold. A list with capacity 6 and two items has a length of 2.

The two matter together when you use [i]. A list only has a slot for each item that has been added, so [i] works for indexes from 0 up to the length minus 1. To give a list all its slots at once, so [i] works for every index up to the capacity, use list_fill. Reading or writing inventory[3] when the list holds only two items is a mistake: The Game Boy Advance module, which the game is built on, checks every index and stops the game with a message naming the bad one. That check is on by default.

The seventeen functions

FunctionWhat it doesIt gives back
list_add(list, value)puts value at the endtrue if it fit, false if the list was full
list_insert(list, index, value)puts value at index and moves the rest alongtrue if it went in, false if index was not a place it could go
list_remove(list, index)takes out the item at indextrue if it removed something, false if index was out of range
list_pop(list)takes out the LAST itemthat item (or a plain 0, false, empty text or nothing if the list was empty)
list_length(list)counts the itemsthe count
list_clear(list)empties the listnothing
list_fill(list, value)grows the list to its capacity, every place holding valuenothing
list_contains(list, value)looks for valuetrue or false
list_find(list, value)looks for value and says where it isits place, or -1 if it is not there
list_reverse(list)turns the order of the items aroundnothing
list_copy(list)makes a second list just like it, with the same roomthe new list
list_create_fill(capacity, value)makes a list that already holds capacity copies of valuethe new list
list_sort(list, ascending)puts the numbers or the text in order, smallest (A) first or biggest (Z) firstnothing
list_shuffle(list)mixes up the order of the itemsnothing
list_foreach(list, script)runs one of your scripts once for each itemnothing
list_filter(list, script)makes a new list of just the items a script says yes tothe new list
list_keep(list, script)takes out of the list itself the items a script says no tonothing
list_sort_by(list, script, ascending)puts the items in order by a number a script works out for each one (works on structs and references too)nothing
list_max(list), list_min(list), list_sum(list)the biggest number, the smallest number, or the total of a list of numbersa number

A few details that decide how you use them:

When a list is full

A list is a box of a fixed size, and a GBA has no spare memory to make it bigger while the game runs. So when a list is full and something more is put in, one item has to be left out. Neither function crashes or stops the game, and the rule is the same in both: whatever would end up past the end is lost.

You do this to a full listWhat happens
list_add(items, x)x is not added, everything already there stays, and you get false
list_insert(items, i, x) where i is before the endx goes in at i, the last item falls off the end and is lost, and you get true
list_insert(items, list_length(items), x) (at the very end)nothing changes, because x is the one that would be past the end, and you get false

So list_add protects what is already in the list, and list_insert protects the new item. This is on purpose, not a bug to work around. If losing the last item would matter, ask first with list_length: compare it to the size you gave the list, and only insert when there is room. Ignoring the true/false answer is fine otherwise; look at it only when the game needs to react, such as showing "inventory full."

The other containers are the same kind of safe, but they are not all the same rule. The "When a container is full" section of Data containers lines them up.

Walking a list

To run one of your scripts on each item in turn, use list_foreach. To do something else with each item, use a for loop with the length and [i]:

/// @type {list<int>}
var scores = [10, 20, 30];
var total = 0;
for (var i = 0; i < list_length(scores); i++) {
    total += scores[i];
}

i starts at 0 and goes up by one until it reaches the length, so the loop body runs once for every item, and never touches an index that does not exist. This is also the way to do anything the sixteen functions do not: there is no function to sort a list of anything but numbers and text, or to find every place a value is at (list_find stops at the first), so those are written as loops over [i].

Four of the sixteen change the whole list at once: list_sort puts a list of numbers or of text in order, list_shuffle mixes up a list of anything, list_reverse turns the order of a list of anything around, and list_keep takes out the items a script says no to. All four work on the list itself and make no copy, so use list_copy first if you want to keep the old list. list_filter leaves the list alone and always gives back a new one, because how many items pass is not known until the game runs; it costs a whole second list, which list_keep does not.

Example

/// @type {list<string>}
/// @capacity 4
var inventory = ["sword"];
list_add(inventory, "shield");
list_add(inventory, "potion");
var count = list_length(inventory);                  // 3
var first = inventory[0];                            // "sword"
list_remove(inventory, 0);                           // "shield" is now first
var has_potion = list_contains(inventory, "potion"); // true

When the list is full, list_add says so:

/// @type {list<int>}
/// @capacity 2
var slots = [1, 2];
var fit = list_add(slots, 3); // false - slots is full, and is unchanged

Handing a list to a script

A script that takes a list as a parameter (see Functions) works on the caller's own list, not on a copy. Adding to it or removing from it inside the script changes the list in the object that called it. Every container works this way, and it is why a script can write list_add(items, x) with no need to hand the list back.

Choosing a list

Reach for a list when items come and go and you want to keep their order. Reach for an array when the count never changes. If items should only ever be added and taken from one end, a stack (last in, first out) or a queue (first in, first out) says so in the code, and the queue never shifts items the way removing from the front of a list does. If you look things up by a name instead of a position, use a map. Data containers compares all seven side by side.

See also: Data containersArrayslist_addlist_insertlist_removelist_poplist_containslist_findlist_reverselist_filllist_copylist_createlist_create_filllist_sortlist_shufflelist_foreachlist_filterlist_keepStructs in data containers